package com.xavier.convertor;

import cn.hutool.core.util.ObjectUtil;
import com.xavier.dto.DeptDTO;
import com.xavier.model.Dept;
import com.xavier.utils.BeanUtil;
import com.xavier.utils.TreeUtil;
import com.xavier.vo.DeptDetailVO;
import com.xavier.vo.DeptPageVO;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 部门对象相互转换
 *
 * @author Xavier
 * @version 1.0
 */
public abstract class DeptConvertor {

    public static DeptDetailVO toDeptDetailVO(Dept dept) {
        if (Objects.isNull(dept)) {
            return null;
        }
        DeptDetailVO vo = BeanUtil.beanCopy(dept, DeptDetailVO.class);
        return vo;
    }

    public static Dept toDept(DeptDTO dto) {
        if (Objects.isNull(dto)) {
            return null;
        }
        Dept dept = BeanUtil.beanCopy(dto, Dept.class);
        return dept;
    }

    public static List<DeptPageVO> toDeptPageVOList(List<Dept> deptList) {
        if (ObjectUtil.isEmpty(deptList)) {
            return new ArrayList<>();
        }
        try {
            return TreeUtil.listToTree(BeanUtil.beanCopy(deptList.stream().sorted(Comparator.comparing(Dept::getSortNumber)).collect(Collectors.toList()), DeptPageVO.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
